package com.example.androidbrowserpro;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import java.util.Locale;

public class SpeechService {
    private static TextToSpeech textToSpeech;
    private static Activity thisActivity;
    public static boolean textToSpeechIsInitialized = false;
    public interface TTSCallback{
        public void over();
    }

    public static TextToSpeech getTextToSpeech() {
        return textToSpeech;
    }

    /**
     * 初始化
     */
    public static void init(Activity activity) {
        thisActivity = activity;
        if (textToSpeech == null) {
            //初始化tts语音
            textToSpeech = new TextToSpeech(activity, new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    // 如果装载TTS引擎成功
                    if (status == TextToSpeech.SUCCESS) {
                        // 设置使用美式英语朗读
                        textToSpeechIsInitialized = true;
                        int result = textToSpeech.setLanguage(Locale.CHINA);
                        // 如果不支持所设置的语言
                        if (result != TextToSpeech.LANG_COUNTRY_AVAILABLE
                                && result != TextToSpeech.LANG_AVAILABLE) {
                            Toast.makeText(activity.getApplicationContext(), "1", Toast.LENGTH_SHORT);
                            //ToastUtils.showShort("该tts不支持中文");
                        }
                        if (textToSpeech != null) {
                            textToSpeech.setPitch(1f);// 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
                            textToSpeech.setSpeechRate(1.0f);

                        }

                    } else {
                        textToSpeech = null;
                    }
                }
            });
        }
    }


    /**
     * 朗读语音
     */
    public static void speakText(String text, TTSCallback callback) {
        if (textToSpeech != null) {
            Toast.makeText(thisActivity, "开始播放", Toast.LENGTH_SHORT);
//            textToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
//                @Override
//                public void onStart(String s) {//开始播放
//                }
//                @Override
//                public void onDone(String s) {//完成之后
//                    callback.over();
//                }
//                @Override
//                public void onError(String s) {//播放错误的处理
//                }
//            });
            textToSpeech.speak(text,TextToSpeech.QUEUE_ADD,null);
//            textToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null,"utteranceld");
//            AudioManager am = (AudioManager) thisActivity.getSystemService(Context.AUDIO_SERVICE);
//            am.setStreamVolume(am.STREAM_MUSIC, 100,0 );
        } else {
            Log.e("test", "语音还未初始化");
            Toast.makeText(thisActivity, "speakText 异常,语音还未初始化", Toast.LENGTH_SHORT);
        }
    }

    /**
     * 关闭并释放资源
     */
    public static void release() {
        if (textToSpeech != null) {
            // 不管是否正在朗读TTS都被打断
            textToSpeech.stop();
            // 关闭，释放资源
            textToSpeech.shutdown();
            textToSpeech = null;
        }
    }

}